<template>
  <el-cascader
    @change="changeValue"
    :value="value"
    size="mini"
    :options="treeData"
    :props="props"
    separator="-"
  ></el-cascader>
</template>

<script>
import {getDepartmentList} from '@/api/department'
import {toTreeDate} from '@/utils'

export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      props: {
        label: 'name', // 要展示的字敦
        value: 'id' // 存储的字段
      },
      treeData: [] // 赋给级联组件的options
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await getDepartmentList()
      this.treeData = toTreeDate(res, 0)
    },
    changeValue(list) {
      // 取到数组的最后一次
      if (list.length > 0) {
        // 将最后一位的id取出来传出去
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null) // 如果长度为0 说明值为空
      }
    }
  }
}
</script>

<style scoped>

</style>
